<template>
	<view class="authorizeddetail" v-show="isauthorizeddetail">
		<view class="overlay" v-if="showPopup"></view>
		<uni-auditingCell
			:detail_type='detail_type'
			:detail='authorizedData'
			:isCheckStep='isCheckStep'
			:checkOption='checkOption'
			:isOrgInfo='isOrgInfo'
			:isAuditing='isAuditing'
			@back='back'
			@eventRules="checkEventRules"
			@checkGroup="checkGroup"
			@toPopupAgree="toPopupAgree"
			@toPopupNotAgree="toPopupNotAgree"
			@toAgree="toAgree"
			>
		</uni-auditingCell>
	</view>
</template>

<script>
	import {authorizedDetail,toAuthorizedEvent,toAuthorizedPlanEvent} from  "@/common/api.js";
	import uniAuditingCell from '@/components/uni-event/uni-auditingcell.vue';
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	export default{
		data(){
			return{
				statusList:[{type:"1",checkStatus:'isAuthorizeding'},{type:"2",checkStatus:'isNoAuthorized'},{type:"3",checkStatus:'isAuthorized'}],  //状态
				id:'',  //赛事ID
				detail_type:'',  //详情类型
				typeID:"",
				authorizedData:{},
				checkOption:[],
				eventRules:'', //竞赛规程
				groupMsg:'' , //小组信息
				isCheckStep:false,  //是否显示审核进度
				isOrgInfo:false, //data是否取eventinfo
				popupData:{},
				groupmd5:"",
				eventmd5:'',
				isauthorizeddetail:false,
				isAuditing:false,
				showPopup:false,
				groupdata:"",  //小组数据
				org_id:""
			}
		},
		onLoad(e) {
			var _this=this;
			_this.id=e.id;
			_this.is_plan=e.is_plan;
			_this.detail_type=_this.statusList.filter(function(item,index){
				return item.type==e.type
			})[0].checkStatus;
			_this.typeID=_this.statusList.filter(function(item,index){
				return item.type==e.type
			})[0].type;
			_this.getAuthorizedDetail()
			uni.showLoading({
			    title: '加载中'
			});
			setTimeout(function(){
					uni.hideLoading();
				_this.isauthorizeddetail=true
			},1500)
		},
		components:{
			uniAuditingCell,
			uniNavBar
		},
		methods:{
			backHome(){
				uni.redirectTo({
					url:"/pages/competitionAudit/event/authorizedEvent?isAudit='false'"
				})
			},
			getAuthorizedDetail(){
				var _this=this,data={
					id:_this.id
				};
				authorizedDetail(data).then(function(res){
					var e=res.data;
					if(e.errcode==0){
							if(e.data.orginfo.length>0){
								_this.isOrgInfo=true;
								// _this.authorizedData['org_id']=e.data.orginfo.length>0?e.data.orginfo[0].org_id:"";
								_this.org_id=e.data.orginfo.length>0?e.data.orginfo[0].org_id:"";
								_this.getOrgDetail()
							}else{
								_this.isOrgInfo=false;
								var list=e.data.eventinfo;
								_this.authorizedData=list;
								// _this.authorizedData['top_level']=e.data.eventinfo.type_name?e.data.eventinfo.type_name:"暂无";
								_this.authorizedData['applyTime']=
								e.data.eventinfo?(e.data.eventinfo.apply_start!=null?
								e.data.eventinfo.apply_start.slice(0,16)+"至"+e.data.eventinfo.apply_end.slice(0,16):"暂无"):"暂无";
								_this.authorizedData['gameTime']=
								e.data.eventinfo?(e.data.eventinfo.game_start!=null?
								e.data.eventinfo.game_start.slice(0,16)+"至"+e.data.eventinfo.game_end.slice(0,16):"暂无"):"暂无";
								_this.authorizedData["deadline"]=e.data.eventinfo.report_deadline!=null?e.data.eventinfo.report_deadline.slice(0,16):"暂无";
								_this.authorizedData['top_level']=e.data.eventinfo.type_name!=null&&e.data.eventinfo.type_name!=""?e.data.eventinfo.type_name:null;
								if(e.data.eventinfo&&e.data.eventconfig==null){
									try{
									  _this.eventRules = decodeURIComponent(e.data.eventinfo.condition)
									}catch(err){
									  _this.eventRules =e.data.eventinfo.condition;
									}
								}
								if(_this.eventRules==""||_this.eventRules==null){
									var authorizedData=JSON.parse(e.data.eventconfig.jsondataevent)
									try{
										_this.eventRules = decodeURIComponent(authorizedData.eventnotice)
									}catch(err){
										_this.eventRules =authorizedData.eventnotice
									}
								}
							}
							// console.log(_this.authorizedData['top_level'])
					}else{
								uni.showToast({
									position: 'center',
									icon: 'none',
									title: e.msg
								});
					}
				})
			},
			getOrgDetail(){
				var _this=this;
				var data={
					id:_this.id,
					org_id:_this.org_id
				}
				authorizedDetail(data).then(function(res){
						var e=res.data;
						if(e.errcode==0){
							// console.log(e.data)
							if(e.data.eventconfig.jsondataevent){
								var list=e.data.eventconfig.jsondataevent,groupList=[];
								_this.authorizedData=JSON.parse(list)
								_this.authorizedData['is_plan']=e.data.eventinfo?e.data.eventinfo.is_plan:"";
								_this.authorizedData['top_level']=e.data.eventinfo.type_name!=null&&e.data.eventinfo.type_name!=""?e.data.eventinfo.type_name:null;
								_this.authorizedData['createTime']=e.data.eventconfig.create_time?e.data.eventconfig.create_time.slice(0,16):"";
								_this.authorizedData['address_all']=(_this.authorizedData.province_name?_this.authorizedData.province_name:"")+(_this.authorizedData.city_name?_this.authorizedData.city_name:"")
																								+(_this.authorizedData.county_name?_this.authorizedData.county_name:"")+(_this.authorizedData.address?_this.authorizedData.address:"")
								_this.authorizedData['linkman']=_this.authorizedData.linkman?_this.authorizedData.linkman:"暂无";
								_this.authorizedData['contact']=_this.authorizedData.contact?_this.authorizedData.contact:"暂无"
								_this.authorizedData['multigroup']=_this.authorizedData.multigroup!=null?(_this.authorizedData.multigroup==true?"允许":"不允许"):null
								_this.authorizedData['isopen']=_this.authorizedData.isopen!=null?(_this.authorizedData.isopen==true?"是":"否"):null
								_this.authorizedData['chessLinkman']=e.data.orginfo.length>0?e.data.orginfo[0].link_name:"暂无";
								_this.authorizedData['chesscontact']=e.data.orginfo.length>0?e.data.orginfo[0].link_phone:"暂无";
								if(e.data.check_logs){
									if(e.data.check_logs.length>0){
										_this.isCheckStep=true;
										e.data.check_logs.map(function(item,index){
											item.createTime=item.create_time?item.create_time.slice(0,16):""
										})
										_this.checkOption=e.data.check_logs;
									}
								}
								if(e.data.check_org_info){
									if(e.data.check_org_info.length>0){
										_this.isCheckStep=true;
										// _this.isEventInfo=true;
										e.data.check_org_info['check_state']=2;
										e.data.check_org_info.map(function(item,index){
											item.createTime=item.create_time?item.create_time.slice(0,16):""
										})
										_this.checkOption.push(e.data.check_org_info)
									}
								}
								groupList=e.data.eventconfig.jsondatagroup!=null?JSON.parse(e.data.eventconfig.jsondatagroup):[];
								_this.authorizedData["signUpCost"]=_this.screenEventCost(groupList["step1"],groupList["step2"])
								// console.log(_this.authorizedData["signUpCost"])
								//竞赛规程
								_this.groupMsg=e.data.eventconfig.jsondatagroup;
								_this.groupmd5=e.data.eventconfig.groupmd5;
								_this.eventmd5=e.data.eventconfig.eventmd5;
								_this.authorizedData['org_name']=e.data.orginfo.length>0?e.data.orginfo[0].org_name:"暂无";
								_this.authorizedData["deadline"]=e.data.eventinfo.report_deadline?e.data.eventinfo.report_deadline.slice(0,16):"暂无";
								//获取竞赛规程和报名小组
								_this.authorizedData['applyTime']=
								e.data.eventinfo?(e.data.eventinfo.apply_start!=null?
								e.data.eventinfo.apply_start.slice(0,16)+"至"+e.data.eventinfo.apply_end.slice(0,16):"暂无"):"暂无";
								_this.authorizedData['gameTime']=
								e.data.eventinfo?(e.data.eventinfo.game_start!=null?
								e.data.eventinfo.game_start.slice(0,16)+"至"+e.data.eventinfo.game_end.slice(0,16):"暂无"):"暂无";
								_this.groupdata=e.data.eventconfig.jsondatagroup!=null?e.data.eventconfig.jsondatagroup:""
							}
							if(e.data.eventinfo&&e.data.eventconfig==null){
								try{
								  _this.eventRules = decodeURIComponent(e.data.eventinfo.condition)
								}catch(err){
								  _this.eventRules =e.data.eventinfo.condition;
								}
							}
							if(_this.eventRules==""||_this.eventRules==null){
								var authorizedData=JSON.parse(e.data.eventconfig.jsondataevent)
								try{
									_this.eventRules = decodeURIComponent(authorizedData.eventnotice)
								}catch(err){
									_this.eventRules =authorizedData.eventnotice
								}
							}
						}else if(e.errcode==1000||e.errcode==1001){
							uni.removeStorageSync('token');
							uni.reLaunch({
								url:'/pages/home/welcome'
							})
						}
					}).catch(function(res){
					uni.showToast({
						position: 'center',
						icon: 'none',
						title: res.msg
					});
				})
			},
			//判断是否显示赛事报名费
			screenEventCost(step,newStep){
				var _this=this,cost=null;
				// console.log(step,newStep)
				if(step && step.length>0){
				  step.map(function(item,index){
						if(item.cost&&item.cost.length>0){
								return cost
						}else{
							if(newStep){
									return cost="￥"+newStep[0]['cost']
							}
						}
					})
				}
				return cost
			},
			//查看竞赛规程
			checkEventRules(){
				var _this=this;
				// console.log("竞赛规程")
				if(_this.eventRules==""||_this.eventRules==null){
					uni.showToast({
						position: 'center',
						icon: 'none',
						title: "暂无竞赛规程"
					});
					return;
				}
				uni.navigateTo({
					url:'/pages/competitionAudit/rules/rules?id='+_this.id+"&org_id="+_this.org_id
				})
			},
			checkGroup(){
				var _this=this;
				if(_this.groupdata==""||_this.groupdata==null){
					uni.showToast({
						position: 'center',
						icon: 'none',
						title: "暂无小组数据"
					});
					return;
				}
				uni.navigateTo({
					url:'/pages/competitionAudit/signUp/signUpGroup?id='+_this.id+"&type="+_this.typeID+"&org_id="+_this.org_id
				})
			},
			//同意授权
			toPopupAgree(){
				var _this=this,data={};
				// console.log(_this.authorizedData.is_plan,_this.authorizedData.orderid)
				if(_this.authorizedData.is_plan==0){
					data={
						eventmd5:_this.eventmd5?_this.eventmd5:"",
						id:_this.id,
						groupmd5:_this.groupmd5?_this.groupmd5:"",
						check_state:2
					}
					toAuthorizedEvent(data).then(function(res){
						var e=res.data
						if(e.errcode==0){
							_this.detail_type='isAuthorized';
							_this.typeID='3';
							_this.getOrgDetail();
							_this.refreshPrepage();
						}else if(e.errcode==1000||e.errcode==1001){
							uni.removeStorageSync('token');
							uni.reLaunch({
								url:'/pages/home/welcome'
							})
						}else{
							uni.showModal({
								title: '',
								content: e.msg,
								showCancel:false,
								confirmText:"确认",
								confirmColor:"#2680EB"
							});
						}
					})
				}else{
					data={
						eventmd5:_this.eventmd5?_this.eventmd5:"",
						id:_this.id,
						groupmd5:_this.groupmd5?_this.groupmd5:"",
						check_state:2,
						order_id:_this.authorizedData.orderid
					}
					toAuthorizedPlanEvent(data).then(function(res){
						var e=res.data
						if(e.errcode==0){
							_this.detail_type='isAuthorized';
							_this.typeID='3';
							_this.getOrgDetail();
							_this.refreshPrepage();
						}else if(e.errcode==1000||e.errcode==1001){
							uni.removeStorageSync('token');
							uni.reLaunch({
								url:'/pages/home/welcome'
							})
						}else{
							uni.showModal({
								title: '',
								content: e.msg,
								showCancel:false,
								confirmText:"确认",
								confirmColor:"#2680EB"
							});
						}
					})
				}
			},
			//拒绝授权
			toPopupNotAgree(txtReason){
				var _this=this,data={};
				if(_this.authorizedData.is_plan==0){
					data={
						id:_this.id,
						remark:txtReason?txtReason:"",
						check_state:"1"
					}
					toAuthorizedEvent(data).then(function(res){
						var e=res.data
						if(e.errcode==0){
							_this.detail_type="isNoAuthorized";
							_this.typeID='2';
							_this.getOrgDetail();
							_this.refreshPrepage();
						}else if(e.errcode==1000||e.errcode==1001){
							uni.removeStorageSync('token');
							uni.reLaunch({
								url:'/pages/home/welcome'
							})
						}else{
							uni.showModal({
								title: '',
								content: e.msg,
								showCancel:false,
								confirmText:"取消",
								confirmColor:"#2680EB"
							});
						}
					})
				}else{
					data={
						id:_this.id,
						remark:txtReason?txtReason:"",
						check_state:"1",
						order_id:_this.authorizedData.orderid
					}
					toAuthorizedPlanEvent(data).then(function(res){
						var e=res.data
						if(e.errcode==0){
							_this.detail_type="isNoAuthorized";
							_this.typeID='2';
							_this.getOrgDetail();
							_this.refreshPrepage();
						}else if(e.errcode==1000||e.errcode==1001){
							uni.removeStorageSync('token');
							uni.reLaunch({
								url:'/pages/home/welcome'
							})
						}else{
							uni.showModal({
								title: '',
								content: e.msg,
								showCancel:false,
								confirmText:"取消",
								confirmColor:"#2680EB"
							});
						}
					})
				}
			},
			toCancel(){
				var _this=this;
				// console.log('zzc')
				_this.showPopup=false;
				_this.popupData.txtReason=""
			},
			refreshPrepage(){
				var pages=getCurrentPages();
				var prePages = pages[pages.length - 2];//获取到上一个页面对象
				if (prePages == undefined || prePages == null) return; 
				prePages.onPullDownRefresh();
			}
		}
	}
</script>

<style lang="less" scoped>
	.authorizeddetail{
		background: #F6F7F9;
		// position: relative;
		// height: 100vh;
	}
	.overlay{
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
		opacity: 0.7;
		background:rgba(0,0,0,0.4);
	}
	.popupTxt{
		background: #fff;
		width: 300px;
		min-height: 150px;
		padding: 10px;
		flex-wrap: wrap;
		font-size: 14px;
	}
</style>
